home *** CD-ROM | disk | FTP | other *** search
/ Clickx 63 / Clickx 63.iso / software / multimedia / mirov204 / Miro_Installer.exe / xulrunner / chrome / toolkit.jar / content / global / printUtils.js < prev    next >
Encoding:
JavaScript  |  2008-03-08  |  11.3 KB  |  303 lines

  1. //@line 41 "/e/xr19rel/WINNT_5.2_Depend/mozilla/toolkit/components/printing/content/printUtils.js"
  2.  
  3. var gPrintSettingsAreGlobal = false;
  4. var gSavePrintSettings = false;
  5. var gFocusedElement = null;
  6.  
  7. var PrintUtils = {
  8.  
  9.   showPageSetup: function ()
  10.   {
  11.     try {
  12.       var printSettings = this.getPrintSettings();
  13.       var PRINTPROMPTSVC = Components.classes["@mozilla.org/embedcomp/printingprompt-service;1"]
  14.                                      .getService(Components.interfaces.nsIPrintingPromptService);
  15.       PRINTPROMPTSVC.showPageSetup(window, printSettings, null);
  16.       if (gSavePrintSettings) {
  17.         // Page Setup data is a "native" setting on the Mac
  18.         var PSSVC = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
  19.                               .getService(Components.interfaces.nsIPrintSettingsService);
  20.         PSSVC.savePrintSettingsToPrefs(printSettings, true, printSettings.kInitSaveNativeData);
  21.       }
  22.     } catch (e) {
  23.       dump("showPageSetup "+e+"\n");
  24.       return false;
  25.     }
  26.     return true;
  27.   },
  28.  
  29.   print: function (aWindow)
  30.   {
  31.     var webBrowserPrint = this.getWebBrowserPrint(aWindow);
  32.     var printSettings = this.getPrintSettings();
  33.     try {
  34.       webBrowserPrint.print(printSettings, null);
  35.       if (gPrintSettingsAreGlobal && gSavePrintSettings) {
  36.         var PSSVC = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
  37.                               .getService(Components.interfaces.nsIPrintSettingsService);
  38.         PSSVC.savePrintSettingsToPrefs(printSettings, true,
  39.                                        printSettings.kInitSaveAll);
  40.         PSSVC.savePrintSettingsToPrefs(printSettings, false,
  41.                                        printSettings.kInitSavePrinterName);
  42.       }
  43.     } catch (e) {
  44.       // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
  45.       // causing an exception to be thrown which we catch here.
  46.       // Unfortunately this will also consume helpful failures, so add a
  47.       // dump("print: "+e+"\n"); // if you need to debug
  48.     }
  49.   },
  50.  
  51.   // calling PrintUtils.printPreview() requires that you have three functions
  52.   // in the global scope: getPPBrowser(), which returns the browser element in
  53.   // the window print preview uses, getNavToolbox(), which returns the element
  54.   // (usually the main toolbox element) before which the print preview toolbar
  55.   // should be inserted, and getWebNavigation(), which returns the document's
  56.   // nsIWebNavigation object
  57.   printPreview: function (aEnterPPCallback, aExitPPCallback, aWindow)
  58.   {
  59.     // if we're already in PP mode, don't set the callbacks; chances
  60.     // are they're null because someone is calling printPreview() to
  61.     // get us to refresh the display.
  62.     var pptoolbar = document.getElementById("print-preview-toolbar");
  63.     if (!pptoolbar) {
  64.       this._onEnterPP = aEnterPPCallback;
  65.       this._onExitPP  = aExitPPCallback;
  66.     } else {
  67.       // collapse the browser here -- it will be shown in
  68.       // onEnterPrintPreview; this forces a reflow which fixes display
  69.       // issues in bug 267422.
  70.       var browser = getPPBrowser();
  71.       if (browser)
  72.         browser.collapsed = true;
  73.     }
  74.  
  75.     this._webProgressPP = {};
  76.     var ppParams        = {};
  77.     var notifyOnOpen    = {};
  78.     var webBrowserPrint = this.getWebBrowserPrint(aWindow);
  79.     var printSettings   = this.getPrintSettings();
  80.     // Here we get the PrintingPromptService so we can display the PP Progress from script
  81.     // For the browser implemented via XUL with the PP toolbar we cannot let it be
  82.     // automatically opened from the print engine because the XUL scrollbars in the PP window
  83.     // will layout before the content window and a crash will occur.
  84.     // Doing it all from script, means it lays out before hand and we can let printing do it's own thing
  85.     var PPROMPTSVC = Components.classes["@mozilla.org/embedcomp/printingprompt-service;1"]
  86.                                .getService(Components.interfaces.nsIPrintingPromptService);
  87.     // just in case we are already printing, 
  88.     // an error code could be returned if the Prgress Dialog is already displayed
  89.     try {
  90.       PPROMPTSVC.showProgress(this, webBrowserPrint, printSettings, this._obsPP, false,
  91.                               this._webProgressPP, ppParams, notifyOnOpen);
  92.       if (ppParams.value) {
  93.         var webNav = getWebNavigation();
  94.         ppParams.value.docTitle = webNav.document.title;
  95.         ppParams.value.docURL   = webNav.currentURI.spec;
  96.       }
  97.  
  98.       // this tells us whether we should continue on with PP or 
  99.       // wait for the callback via the observer
  100.       if (!notifyOnOpen.value.valueOf() || this._webProgressPP.value == null)
  101.         this.enterPrintPreview();
  102.     } catch (e) {
  103.       this.enterPrintPreview();
  104.     }
  105.   },
  106.  
  107.   getWebBrowserPrint: function (aWindow)
  108.   {
  109.     var contentWindow = aWindow || window.content;
  110.     return contentWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  111.                         .getInterface(Components.interfaces.nsIWebBrowserPrint);
  112.   },
  113.  
  114.   ////////////////////////////////////////
  115.   // "private" methods. Don't use them. //
  116.   ////////////////////////////////////////
  117.  
  118.   setPrinterDefaultsForSelectedPrinter: function (aPSSVC, aPrintSettings)
  119.   {
  120.     if (!aPrintSettings.printerName)
  121.       aPrintSettings.printerName = aPSSVC.defaultPrinterName;
  122.  
  123.     // First get any defaults from the printer 
  124.     aPSSVC.initPrintSettingsFromPrinter(aPrintSettings.printerName, aPrintSettings);
  125.     // now augment them with any values from last time
  126.     aPSSVC.initPrintSettingsFromPrefs(aPrintSettings, true,  aPrintSettings.kInitSaveAll);
  127.   },
  128.  
  129.   getPrintSettings: function ()
  130.   {
  131.     var pref = Components.classes["@mozilla.org/preferences-service;1"]
  132.                          .getService(Components.interfaces.nsIPrefBranch);
  133.     if (pref) {
  134.       gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
  135.       gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
  136.     }
  137.  
  138.     var printSettings;
  139.     try {
  140.       var PSSVC = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
  141.                             .getService(Components.interfaces.nsIPrintSettingsService);
  142.       if (gPrintSettingsAreGlobal) {
  143.         printSettings = PSSVC.globalPrintSettings;
  144.         this.setPrinterDefaultsForSelectedPrinter(PSSVC, printSettings);
  145.       } else {
  146.         printSettings = PSSVC.newPrintSettings;
  147.       }
  148.     } catch (e) {
  149.       dump("getPrintSettings: "+e+"\n");
  150.     }
  151.     return printSettings;
  152.   },
  153.  
  154.   _closeHandlerPP: null,
  155.   _webProgressPP: null,
  156.   _onEnterPP: null,
  157.   _onExitPP: null,
  158.  
  159.   // This observer is called once the progress dialog has been "opened"
  160.   _obsPP: 
  161.   {
  162.     observe: function(aSubject, aTopic, aData)
  163.     {
  164.       // delay the print preview to show the content of the progress dialog
  165.       setTimeout(function () { PrintUtils.enterPrintPreview(); }, 0);
  166.     },
  167.  
  168.     QueryInterface : function(iid)
  169.     {
  170.       if (iid.equals(Components.interfaces.nsIObserver) ||
  171.           iid.equals(Components.interfaces.nsISupportsWeakReference) ||
  172.           iid.equals(Components.interfaces.nsISupports))
  173.         return this;   
  174.       throw Components.results.NS_NOINTERFACE;
  175.     }
  176.   },
  177.  
  178.   enterPrintPreview: function (aWindow)
  179.   {
  180.     gFocusedElement = document.commandDispatcher.focusedElement;
  181.  
  182.     var webBrowserPrint = this.getWebBrowserPrint(aWindow);
  183.     var printSettings   = this.getPrintSettings();
  184.     try {
  185.       webBrowserPrint.printPreview(printSettings, null, this._webProgressPP.value);
  186.     } catch (e) {
  187.       // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
  188.       // causing an exception to be thrown which we catch here.
  189.       // Unfortunately this will also consume helpful failures, so add a
  190.       // dump(e); // if you need to debug
  191.       return;
  192.     }
  193.  
  194.     var printPreviewTB = document.getElementById("print-preview-toolbar");
  195.     if (printPreviewTB) {
  196.       printPreviewTB.updateToolbar();
  197.       var browser = getPPBrowser();
  198.       if (browser)
  199.         browser.collapsed = false;
  200.       return;
  201.     }
  202.  
  203.     // show the toolbar after we go into print preview mode so
  204.     // that we can initialize the toolbar with total num pages
  205.     var XUL_NS =
  206.       "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  207.     printPreviewTB = document.createElementNS(XUL_NS, "toolbar");
  208.     printPreviewTB.setAttribute("printpreview", true);
  209.     printPreviewTB.setAttribute("id", "print-preview-toolbar");
  210.  
  211.     var navToolbox = getNavToolbox();
  212.     navToolbox.parentNode.insertBefore(printPreviewTB, navToolbox);
  213.  
  214.     // copy the window close handler
  215.     if (document.documentElement.hasAttribute("onclose"))
  216.       this._closeHandlerPP = document.documentElement.getAttribute("onclose");
  217.     else
  218.       this._closeHandlerPP = null;
  219.     document.documentElement.setAttribute("onclose", "PrintUtils.exitPrintPreview(); return false;");
  220.  
  221.     // disable chrome shortcuts...
  222.     window.addEventListener("keypress", this.onKeyPressPP, true);
  223.  
  224.     var contentWindow = aWindow || window.content;
  225.     contentWindow.focus();
  226.  
  227.     // on Enter PP Call back
  228.     if (this._onEnterPP) {
  229.       this._onEnterPP();
  230.       this._onEnterPP = null;
  231.     }
  232.   },
  233.  
  234.   exitPrintPreview: function (aWindow)
  235.   {
  236.     window.removeEventListener("keypress", this.onKeyPressPP, true);
  237.  
  238.     // restore the old close handler
  239.     document.documentElement.setAttribute("onclose", this._closeHandlerPP);
  240.     this._closeHandlerPP = null;
  241.  
  242.     var webBrowserPrint = this.getWebBrowserPrint(aWindow);
  243.     webBrowserPrint.exitPrintPreview(); 
  244.  
  245.     // remove the print preview toolbar
  246.     var printPreviewTB = document.getElementById("print-preview-toolbar");
  247.     getNavToolbox().parentNode.removeChild(printPreviewTB);
  248.  
  249.     var contentWindow = aWindow || window.content;
  250.     contentWindow.focus();
  251.  
  252.     var cmdDispatcher = document.commandDispatcher;
  253.     cmdDispatcher.suppressFocusScroll = true;
  254.     if (gFocusedElement instanceof HTMLElement ||
  255.         gFocusedElement instanceof XULElement ||
  256.         gFocusedElement instanceof Window) {
  257.       gFocusedElement.focus();
  258.     }
  259.     else if (gFocusedElement instanceof Node) {
  260.       var content = window.content;
  261.       if (content instanceof Components.interfaces.nsIInterfaceRequestor)
  262.         content.getInterface(Components.interfaces.nsIDOMWindowUtils).focus(gFocusedElement);
  263.       }
  264.     gFocusedElement = null;
  265.     cmdDispatcher.suppressFocusScroll = false;
  266.  
  267.     // on Exit PP Call back
  268.     if (this._onExitPP) {
  269.       this._onExitPP();
  270.       this._onExitPP = null;
  271.     }
  272.   },
  273.  
  274.   onKeyPressPP: function (aEvent)
  275.   {
  276.     var closeKey;
  277.     try {
  278.       closeKey = document.getElementById("key_close")
  279.                          .getAttribute("key");
  280.       closeKey = aEvent["DOM_VK_"+closeKey];
  281.     } catch (e) {}
  282.     var isModif = aEvent.ctrlKey || aEvent.metaKey;
  283.     // ESC and Ctrl-W exits the PP
  284.     if (aEvent.keyCode == aEvent.DOM_VK_ESCAPE || isModif &&
  285.         (aEvent.charCode == closeKey || aEvent.charCode == closeKey + 32)) {
  286.       PrintUtils.exitPrintPreview();
  287.     }
  288.     else if (isModif) {
  289.       var printPreviewTB = document.getElementById("print-preview-toolbar");
  290.       var printKey = document.getElementById("printKb").getAttribute("key").toUpperCase();
  291.       var pressedKey = String.fromCharCode(aEvent.charCode).toUpperCase();
  292.       if (printKey == pressedKey) {
  293.       PrintUtils.print();
  294.       }
  295.     }
  296.     // cancel shortkeys
  297.     if (isModif) {
  298.       aEvent.preventDefault();
  299.       aEvent.stopPropagation();
  300.     }
  301.   }
  302. }
  303.